﻿using System;
using System.Collections;
using System.Collections.Generic;
//using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;

namespace Reports
{
    /// <summary>
    /// 邮件相关的配置
    /// </summary>
    public static partial class MailConfig
    {
        #region 私有变量
        private static readonly string configpath = BaseConfig.BasePath + "email.config";
        private static string _mode;
        private static string _host;
        private static int _port;
        private static string _from;
        private static string _username;
        private static string _password;
        private static bool _enablessl;


        //private static readonly string configpath = (UiConfig.RootDir + "/config/email/email.config"); 

        #endregion

        #region 
        private static List<Report> rptlist = new List<Report>();

        public static List<Report> Rptlist
        {
            get { return MailConfig.rptlist; }
            set { MailConfig.rptlist = value; }
        }


        public static Hashtable ht_columnsmap = new Hashtable();
        #endregion

        /// <summary>
        /// 静态构造函数
        /// </summary>
        static MailConfig()
        {
            Reload();
        }



        #region  smtp 属性
        /// <summary>
        /// 重新读取Config文件，重新取值
        /// </summary>
        public static void Reload()
        {
            string filepath = configpath;
            XmlDocument xml = new XmlDocument();
            xml.Load(filepath);
            XmlNode rootpor = xml.SelectSingleNode("email/setting");
            _mode = rootpor.Attributes["current"].Value;
            foreach (XmlNode n in rootpor.ChildNodes)
            {
                if (n.NodeType != XmlNodeType.Comment && n.Name.ToLower() == "parameter")
                {
                    _host = n.Attributes["host"].Value;
                    _port = int.Parse(n.Attributes["port"].Value);
                    _from = n.Attributes["from"].Value;
                    _username = n.Attributes["username"].Value;
                    _password = n.Attributes["password"].Value;
                    if (n.Attributes["enablessl"].Value.ToLower() == "true")
                        _enablessl = true;
                    else
                        _enablessl = false;
                }
            }


            #region 需要发的报表情况
            if (rptlist.Count > 0)
                rptlist.Clear();

            Report rptmodel;

            if (BaseConfig.If_Test)
                rootpor = xml.SelectSingleNode("email/Reports");
            else
                rootpor = xml.SelectSingleNode("email/Demo");

            foreach (XmlNode n in rootpor.ChildNodes)
            {
                if (n.NodeType != XmlNodeType.Comment && n.Name.ToLower() == "report")
                {
                    rptmodel = new Report();

                    rptmodel.Day = n.Attributes["Day"].Value;
                    rptmodel.Hour = n.Attributes["Hour"].Value;

                    //rptmodel.RptName = n.Attributes["Name"].Value.Replace("{now}", DateTime.Now.ToString("yyyyMMdd"));
                    rptmodel.RptName = n.Attributes["Name"].Value;
                    rptmodel.RptTo = n.Attributes["To"].Value;
                    rptmodel.SQLString = n.Attributes["SQLString"].Value;
                    rptmodel.SqlType = n.Attributes["SQLType"].Value;
                    rptmodel.DB = n.Attributes["DB"].Value.ToUpper();
                    rptmodel.Enable = (n.Attributes["Enable"].Value != null && n.Attributes["Enable"].Value.Equals("1"));

                    rptlist.Add(rptmodel);
                }
            }
            #endregion

            #region 报表字段映射
            rootpor = xml.SelectSingleNode("email/columnsMap");
            ht_columnsmap.Clear();

            foreach (XmlNode n in rootpor.ChildNodes)
            {
                if (n.NodeType != XmlNodeType.Comment && n.Name.ToLower() == "columns")
                {

                    ht_columnsmap.Add(n.Attributes["from"].Value.ToUpper(), n.Attributes["to"].Value);

                }
            }
            #endregion

            //new  Exception();

        }

        /// <summary>
        /// 邮件组件方式
        /// </summary>
        public static string mode
        {
            get { return _mode; }
        }
        /// <summary>
        /// 服务器
        /// </summary>
        public static string host
        {
            get { return _host; }
        }
        /// <summary>
        /// 端口
        /// </summary>
        public static int port
        {
            get { return _port; }
        }
        /// <summary>
        /// 发送邮件地址
        /// </summary>
        public static string from
        {
            get { return _from; }
        }
        /// <summary>
        /// 用户名
        /// </summary>
        public static string username
        {
            get { return _username; }
        }
        /// <summary>
        /// 密码
        /// </summary>
        public static string password
        {
            get { return _password; }
        }
        /// <summary>
        /// SSL加密
        /// </summary>
        public static bool enablessl
        {
            get { return _enablessl; }
        }

        /// <summary>
        /// 密码找回的邮件内容
        /// </summary>
        public static string EmailContentTemp
        {
            get { return GetConfigValue("matter/emailcontenttemp", configpath, true); }
        }
        #endregion

        /// <summary>
        /// 取得邮箱对应的登录网页地址
        /// </summary>
        /// <param name="emailAddress"></param>
        /// <returns></returns>
        public static string GetEmailLoginUrl(string emailAddress)
        {
            if (emailAddress != null)
            {
                string str = Regex.Match(emailAddress, "@.+$", RegexOptions.Compiled).Value;
                if ((str == null) || (str.Trim() == string.Empty))
                {
                    return string.Empty;
                }
                string filename = configpath;
                XmlDocument document = new XmlDocument();
                document.Load(filename);
                XmlNode node = document.SelectSingleNode("email/loginurl");
                foreach (XmlNode node2 in node.ChildNodes)
                {
                    if (((node2.NodeType != XmlNodeType.Comment) && (node2.Name.ToLower() == "email")) 
                        && (node2.Attributes["address"].Value.ToLower() == str.ToLower()))
                    {
                        return node2.InnerText;
                    }
                }
            }
            return string.Empty;
        }


        /// <summary>
        /// 读取配置文件
        /// </summary>
        /// <param name="Target"></param>
        /// <param name="ConfigPathName"></param>
        /// <returns></returns>
        public static string GetConfigValue(string Target, string XmlPath, params bool[] cdata)//internal
        {
            System.Xml.XmlDocument xdoc = new XmlDocument();
            xdoc.Load(XmlPath);
            XmlNode node = xdoc.DocumentElement.SelectSingleNode(Target);
            if (node != null)
            {
                if ((cdata != null) && cdata[0])
                {
                    return node.InnerText;
                }
                return node.InnerXml;
            }
            return string.Empty;
        }

    }


    public class Report
    {
        //<RptName>测试</RptName>
        //   <RptTo>314900540@qq.com</RptTo>
        //   <SQL type="sql"></SQL>
        
        //<Report 
        //    Enable = "1" 
        //    Name="用户表测试{now}"  
        //    Day="0,1,2,3,4,5,6" 
        //    Hour="2"
        //    To="314900540@qq.com" 
        //    DB="web" 
        //    SQLType="sql"
        //    SQLString="SELECT F_Account,F_RealName,F_Gender,F_MobilePhone  FROM [dbo].[Sys_User] " />

        private string _Day;

        public string Day
        {
            get { return _Day; }
            set { _Day = value; }
        }
        private string _Hour;

        public string Hour
        {
            get { return _Hour; }
            set { _Hour = value; }
        }

        private string _RptName;

        public string RptName
        {
            get { return _RptName; }
            set { _RptName = value; }
        }

        private string _RptTo;

        public string RptTo
        {
            get { return _RptTo; }
            set { _RptTo = value; }
        }

        private string _SQLString;

        public string SQLString
        {
            get { return _SQLString; }
            set { _SQLString = value; }
        }

        private string _SqlType;

        public string SqlType
        {
            get { return _SqlType; }
            set { _SqlType = value; }
        }

        private string _DB;

        public string DB
        {
            get { return _DB; }
            set { _DB = value; }
        }

        private bool _Enable;

        public bool Enable
        {
            get { return _Enable; }
            set { _Enable = value; }
        }
    }


}
